package ac.man.cs.afzal.tcm.terms_sentence_handler;


import ac.man.cs.afzal.tcm.nlp.File_Manager_Impl;
import ac.man.cs.afzal.tcm.nlp.Vector_Builder_Impl;
import ac.man.cs.afzal.tcm.terms_sentence_handler.Clist_to_Occurrences_Generator;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Vector;

/**
 * Terms_Generator_Imp class extracts the Terms from "clist" files. Two parameters, CLISTS_DIR and TERMS_DIR can be set in properties file.
 *
 * @author Hammad Afzal
 * @version 1.0
 * Last modification date: 2nd May, 2012
 */
public class Clist_to_Terms_Generator
{

    private String CLISTS_DIR = "CLISTS_DIR";
    private String TERMS_DIR = "TERMS_DIR";
    private File_Manager_Impl fileManager = new File_Manager_Impl();
    private Vector_Builder_Impl vectorBuilder = new Vector_Builder_Impl();

    /* CLIST files are generated from the Termine, a software
    for automatic term recognition. It contains the terms and their occurrences
     */
    public Clist_to_Terms_Generator()
    {
        Properties props = new Properties();
        try
        {
            props.load(new FileInputStream("Term_Classification.properties"));
        }
        catch (IOException ex)
        {
            Logger.getLogger(Clist_to_Terms_Generator.class.getName()).log(Level.SEVERE, null, ex);
        }
        CLISTS_DIR = props.getProperty(CLISTS_DIR);
        TERMS_DIR = props.getProperty(TERMS_DIR);
    }

    public void terms_extractor()
    {
        File clists_dir = new File(CLISTS_DIR);
        for (String clists_file : clists_dir.list())
        {
            System.out.println("File: " + clists_file + "being processed");
            if (!(clists_file.startsWith("."))) // Check for hidden folder, e.g. svn
            {
                try
                {
                    BufferedReader in = new BufferedReader(new FileReader(CLISTS_DIR + clists_file));
                    Vector<String> terms_vector = new Vector();
                    String str = "";
                    while ((str = in.readLine()) != null)
                    {
                        str = str.substring(0, str.lastIndexOf("\""));
                        str = str.substring(str.lastIndexOf("\"")+1,str.length());
                        if(str.contains("."))
                            System.out.println("Term containing illegal character :" + str);
                        else
                            terms_vector.add(str);
                    }

                    String output_contents = vectorBuilder.vector_to_string_with_items_list(terms_vector);

                    String input_file_without_extension = fileManager.strip_file_name_from_file_name_plus_extension(clists_file, "clist");
                    String output_file = input_file_without_extension + ".terms";
                    BufferedWriter out = new BufferedWriter(new FileWriter(TERMS_DIR + output_file));
                    out.write(output_contents);
                    out.close();

                }

                catch (FileNotFoundException ex)
                {
                    Logger.getLogger(Clist_to_Occurrences_Generator.class.getName()).log(Level.SEVERE, null, ex);
                }                catch (IOException ex)
                {
                    Logger.getLogger(Clist_to_Occurrences_Generator.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.out.println("File: " + clists_file + " Processed");
        }
    }
}
